<?php

namespace App\Console\Commands;

use App\Article;
use Illuminate\Console\Command;

class SortArticle extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sort:article';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '整理文章 让文章归属不同的tag';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        Article::chunk(200, function ($articles) {
            foreach ($articles as $ar) {
                $tag_ids = $ar->tags->pluck('id')->toArray();
                $tag_ids = array_unique($tag_ids);
                $ar->tags()->sync($tag_ids);
                $ar->save();
            }
        });
    }
}
